<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        .box {
            position: relative;
            width: 178px;
            margin: 100px;
        }
        .con {
            display: none;
            position: absolute;
            top: -40px;
            width: 171px;
            border: 1px solid rgba(0, 0, 0, .2);
            box-shadow: 0 2px 4px rgba(0, 0, 0, .2);
            padding: 5px 0;
            font-size: 18px;
            line-height: 20px;
            color: #333;
        }
        .con::before{
            content: '';
            width: 0;
            height: 0;
            position: absolute;
            top: 28px;
            left: 18px;
            border: 8px solid #000;
            border-style: solid dashed dashed;
            border-color:#fff transparent transparent;
        }
    </style>
</head>

<body>
    <input type="text">
    <div class="box">
        <div class="con">123</div>
        <input type="text" class='jd' placeholder="请输入你的快递单号">
    </div>
    <script>
        //onkeyup 松开触发
        // onkeydown 按下触发
        // onkeypress 按下触发 但不识别功能键 区分大小写
        // keyup和keydown 不区分大小写
        document.addEventListener('keyup', function (e) {
            console.log('弹起来啦');
            // console.log(e.key);  //key为按下的键值，但很多浏览器不兼容
            console.log(e.keyCode); //keyCode为按下键的ascll码值            
        })
        document.addEventListener('keydown', function () {
            console.log('在按down了');
        })
        document.addEventListener('keypress', function (e) {
            console.log('我按下了press');
            console.log(e.keyCode);

        })
        //执行顺序  keydown -- keypress -- keyup

        //按下s键，定位到搜索框
        var search = document.querySelector('input');
        document.addEventListener('keyup', function (e) {
            if (e.keyCode == 83) {
                search.focus();
            }
        })

        //京东大写案例
        var con =document.querySelector('.con');
        var jd =document.querySelector('.jd');
        //这里用keydown和keypress不行  因为文字后于事件显示
        //即 keydown -> display='' -> 文字落入框内
        jd.addEventListener('keyup',function(e){
            con.innerHTML=jd.value;
            con.style.display='block';
            if(jd.value==''){
                con.style.display='';
            }
        })
        jd.addEventListener('blur',function(e){
            con.style.display='none';
        })
        jd.addEventListener('focus',function(e){
            if(this.value!=''){
                con.style.display='block';
            }
        })
    </script>
</body>

</html>